package com.yanqu.road.entity.activity.springdraw.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;

import java.util.Map;

public class SpringDrawConfig {

    // 活动信息
    private ActivityInfo activityInfo;

    //积分ID（迎春花
    private int EXCHANGE_SCORE_ITEM_ID;

    //消耗道具ID（花浇）
    private int SPRING_DRAW_ITEM_ID;

    //美女服装预览
    private int SHOW_SKIN_BEAUTY;

    //藏宝预览
    private int SHOW_CURIOITEM;

    //门客服装预览
    private int SHOW_SKIN_PARTON;

    //美女预览
    private int SHOW_BEAUTY;

    //进入下一关所需连线
    private int SPRING_DRAW_LINE;

    //关卡配置
    private Map<Integer, SpringDrawDigRewardConfig> digRewardConfigMap;

    //最后一关的奖励
    private SpringDrawDigRewardConfig endLevelReward;

    //十连解锁条件(浇灌次数)
    private int SPRING_DRAW_TEN_GACHA_UNLOCK;

    //每个花浇的获取的积分区间(下限;上限) 下线：SPRING_DRAW_GACHA_SCORE[0]，上限：SPRING_DRAW_GACHA_SCORE[1]
    private int[] SPRING_DRAW_GACHA_SCORE;

    //棋盘大小,策划说写死5 * 5
    private int boardSize = 5;

    /**
     * 构造函数
     * @param activityInfo
     * @param activityConfigMap
     */
    public SpringDrawConfig(ActivityInfo activityInfo, Map<String, ActivityConfig> activityConfigMap, Map<Integer, SpringDrawDigRewardConfig> digRewardConfigMap) {
        this.activityInfo = activityInfo;
        this.EXCHANGE_SCORE_ITEM_ID = activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue();
        this.SPRING_DRAW_ITEM_ID = activityConfigMap.get("SPRING_DRAW_ITEM_ID").getIntValue();
        this.SHOW_SKIN_BEAUTY = activityConfigMap.get("SHOW_SKIN_BEAUTY").getIntValue();
        this.SHOW_CURIOITEM = 0;
        this.SHOW_SKIN_PARTON = activityConfigMap.get("SHOW_SKIN_PARTON").getIntValue();
        this.SHOW_BEAUTY = activityConfigMap.get("SHOW_BEAUTY").getIntValue();
        this.SPRING_DRAW_LINE = activityConfigMap.get("SPRING_DRAW_LINE").getIntValue();
        this.SPRING_DRAW_TEN_GACHA_UNLOCK = activityConfigMap.get("SPRING_DRAW_TEN_GACHA_UNLOCK").getIntValue();
        //获取积分的区间值
        String scoreQj = activityConfigMap.get("SPRING_DRAW_GACHA_SCORE").getValue();
        String[] scoreQjStrArr = scoreQj.split(";");
        int[] scoreQjArr = new int[scoreQjStrArr.length];
        scoreQjArr[0] = Integer.parseInt(scoreQjStrArr[0].trim());
        scoreQjArr[1] = Integer.parseInt(scoreQjStrArr[1].trim());
        this.SPRING_DRAW_GACHA_SCORE = scoreQjArr;
        this.digRewardConfigMap = digRewardConfigMap;
        //获取最后一关的奖励
        for (SpringDrawDigRewardConfig drawDigRewardConfig : digRewardConfigMap.values()) {
            if (this.endLevelReward == null || drawDigRewardConfig.getLevel() > this.endLevelReward.getLevel()) {
                this.endLevelReward = drawDigRewardConfig;
            }
        }
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getEXCHANGE_SCORE_ITEM_ID() {
        return EXCHANGE_SCORE_ITEM_ID;
    }

    public int getSPRING_DRAW_ITEM_ID() {
        return SPRING_DRAW_ITEM_ID;
    }

    public int getSHOW_SKIN_BEAUTY() {
        return SHOW_SKIN_BEAUTY;
    }

    public int getSHOW_CURIOITEM() {
        return SHOW_CURIOITEM;
    }

    public int getSHOW_SKIN_PARTON() {
        return SHOW_SKIN_PARTON;
    }

    public int getSHOW_BEAUTY() {
        return SHOW_BEAUTY;
    }

    public int getSPRING_DRAW_LINE() {
        return SPRING_DRAW_LINE;
    }

    public int getSPRING_DRAW_TEN_GACHA_UNLOCK() {
        return SPRING_DRAW_TEN_GACHA_UNLOCK;
    }

    public Map<Integer, SpringDrawDigRewardConfig> getDigRewardConfigMap() {
        return digRewardConfigMap;
    }

    /**
     * 获取关卡配置
     */
    public SpringDrawDigRewardConfig getDigRewardConfig(int level) {
        Map<Integer, SpringDrawDigRewardConfig> map = this.getDigRewardConfigMap();
        if (map != null) {
            SpringDrawDigRewardConfig levelConfig = map.get(level);
            if (levelConfig != null) {
                return levelConfig;
            } else {
                //大于配置最高关卡，默认读取最后一关
                if (level > map.size()) {
                    return this.endLevelReward;
                }
            }
        }
        return null;
    }

    /**
     * 获取新棋盘
     * @return
     */
    public int[][] getNewBoard() {
        return new int[boardSize][boardSize];
    }

    /**
     * 棋盘大小
     * @return
     */
    public int getBoardSize() {
        return boardSize;
    }

    /**
     * 每个花浇增加的积分
     * @return Property
     */
    public long costOnePropAddScore() {
        RandomHelper randomHelper = new RandomHelper();
        return randomHelper.next(this.SPRING_DRAW_GACHA_SCORE[0], this.SPRING_DRAW_GACHA_SCORE[1] + 1);
    }

    /**
     * 根据消耗数量计算并返回增加的分数
     * @param costNum 消耗数量
     * @return 增加的分数
     */
    public long drawOnceAddScore(int costNum) {
        long addScore = 0;
        for (int i = 0; i < costNum; i++) {
            addScore += this.costOnePropAddScore();
        }
        return addScore;
    }
}
